﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SpriteNum : MonoBehaviour {

    public Sprite[] numsSprite;

    int m_Num;
    public int num
    {
        get { return m_Num; }
        set
        {
            m_Num = value;
            
            ShowValue(m_Num);
        }
    }
    public Text numText;
    public SpriteRenderer unitSR;
    public SpriteRenderer decadeSR;

    public Image unitImg;
    public Image decadeImg;

    // Use this for initialization
    void Start () {
		
	}

    public int testShowNum;
    [ContextMenu("TestShowValue")]
    void TestShowValue()
    {
        ShowValue(testShowNum);
    }
    
    void ShowValue(int num)
    {
        if (numText)
        {
            numText.text = num.ToString();
            return;
        }
        int decade = num / 10;
        int unit = num % 10;

        if(decade >= numsSprite.Length
            || unit >= numsSprite.Length)
        {
            GameDebug.LogError($"decade>= numsSprite.Length   decade:{decade}  unit:{unit}");
            return;
        }
        if(decadeSR)
            decadeSR.sprite = numsSprite[decade];
        if (unitSR)
            unitSR.sprite = numsSprite[unit];

        if (decadeImg)
            decadeImg.sprite = numsSprite[decade];
        if (unitImg)
            unitImg.sprite = numsSprite[unit];

    }
    
	
	// Update is called once per frame
	void Update () {
		
	}
}
